package gui;

import javax.swing.text.AttributeSet;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Element;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;

public class EditorDocument extends DefaultStyledDocument {

	private static final long serialVersionUID = 1L;

	public void setBold(int startPosition, int endPosition) {
		for (int i = startPosition; i < endPosition; i++)
			setBold(getSimpleAttributeSet(i), i);
	}

	public void setItalic(int startPosition, int endPosition) {
		for (int i = startPosition; i < endPosition; i++)
			setItalic(getSimpleAttributeSet(i), i);
	}

	public void setUnderline(int startPosition, int endPosition) {
		for (int i = startPosition; i < endPosition; i++)
			setUnderline(getSimpleAttributeSet(i), i);
	}

	private void setItalic(SimpleAttributeSet sas, int pos) {
		if (StyleConstants.isItalic(sas))
			StyleConstants.setItalic(sas, false);
		else
			StyleConstants.setItalic(sas, true);
		setCharacterAttributes(pos, 1, sas, true);
	}

	private void setBold(SimpleAttributeSet sas, int pos) {
		if (StyleConstants.isBold(sas))
			StyleConstants.setBold(sas, false);
		else
			StyleConstants.setBold(sas, true);
		setCharacterAttributes(pos, 1, sas, true);
	}

	private void setUnderline(SimpleAttributeSet sas, int pos) {
		if (StyleConstants.isUnderline(sas))
			StyleConstants.setUnderline(sas, false);
		else
			StyleConstants.setUnderline(sas, true);
		setCharacterAttributes(pos, 1, sas, true);
	}

	public SimpleAttributeSet getSimpleAttributeSet(int pos) {
		Element ele = getCharacterElement(pos);
		AttributeSet as = ele.getAttributes();
		SimpleAttributeSet sas = new SimpleAttributeSet(as);
		return sas;
	}

	public EditorDocument() {
		super();
	}

	public EditorDocument(Content arg0, StyleContext arg1) {
		super(arg0, arg1);
	}

	public EditorDocument(StyleContext arg0) {
		super(arg0);
	}
}